package cn.niit.pm.config;

import cn.niit.pm.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/*
 * 对拦截器进行配置注册
 * */
@Configuration  //告诉Spring容器这是一个注解类
public class WebMVCConfig implements WebMvcConfigurer {
    /** 此处是读取 springboot 配置文件（application.properties）中的元数据，实现拦截路径
     以及排除路径的动态配置 */
    @Value("${springmvc.interceptor.exclude-path-patterns}")
    private List<String> EXCLUDE_PATH_PATTERNS;
    /*
     *  注册拦截器
     * */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("EXCLUDE_PATH_PATTERNS=" + EXCLUDE_PATH_PATTERNS);
        //注册拦截器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
//        //先拦截所有请求
        registration.addPathPatterns("/**");
//        /**
//         * 设置拦截器白名单：将以下请求排除在拦截目录范围内
//         */
        registration.excludePathPatterns(EXCLUDE_PATH_PATTERNS);
    }
}
